<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="兑换人：">
          <el-input v-model="listPram.phone" placeholder="输入手机号" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item label="发货状态：">
          <el-select v-model="listPram.status" placeholder="请选择" style="width: 200px;">
            <el-option
                v-for="item in statusList"
                :key="item.id"
                :label="item.data"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地区：" prop="areaId">
          <el-cascader class="col"
                       placeholder="可输入地区名称进行搜索"
                       v-model="value"
                       :options="options"
                       clearable
                       :props="{ expandTrigger: 'hover' }"
                       filterable
                       @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>

        <el-form-item style="margin-left:100px;">
          <span style="margin-right: 10px">总代金券收入 {{dataStatistics.sum1}}</span>
          <span style="margin-right: 10px">总积分收入 {{dataStatistics.integral}}</span>
          <span  style="margin-right: 10px">总销量 {{dataStatistics.count1}}</span>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" size="mini"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column label="ID" prop="id" coverImg="120"></el-table-column>
        <el-table-column label="商品名称" prop="goodsName" min-width="130"/>
        <el-table-column label="商品类型" min-width="130" prop="inventory">
          <template slot-scope="scope">
            <div v-if="scope.row.goodsType==1">线上</div>
            <div v-else>线下</div>
          </template>
        </el-table-column>
        <el-table-column label="兑换人" min-width="130" prop="inventory">
          <template slot-scope="scope">
            <div>{{ scope.row.user.phone }}</div>
          </template>
        </el-table-column>
        <el-table-column label="实付" min-width="130" prop="integral">
          <template slot-scope="scope">
            <div v-if="scope.row.voucher">
              {{ scope.row.voucher + '益联券' }}+{{ scope.row.integral + '积分' }}
            </div>
            <div v-else-if="scope.row.integral">
              {{ scope.row.integral + '积分' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="购买数量" min-width="50" prop="number"/>
        <el-table-column label="状态" min-width="50" prop="status">
          <template slot-scope="scope">
            <div style="color:#022cff;" v-if="scope.row.status===1">待发货</div>
            <div style="color:#f6ce4f;" v-if="scope.row.status===2">待收货</div>
            <div style="color:#02ff4a;" v-if="scope.row.status===3">已完成</div>
            <div style="color:#f50d2f;" v-if="scope.row.status===4">已申请售后</div>
            <div style="color:#f50d2f;" v-if="scope.row.status===5">已拒绝</div>
            <div style="color:#f50d2f;" v-if="scope.row.status===6">已退款</div>
            <div style="color:#1b77fd;" v-if="scope.row.status===7">待线下核销</div>
          </template>
        </el-table-column>
        <el-table-column label="购买时间" prop="createTime" min-width="100"/>
        <el-table-column label="操作" min-width="180" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" v-if="scope.row.status===1" size="mini" type="text"
                       @click="openEdit(scope.row.id)" v-has-permi="['admin:integralOrder:send']">发货
            </el-button>
            <el-button icon="el-icon-edit"
                       v-else-if="scope.row.status!==1 && scope.row.status===2" size="mini"
                       type="text"
                       @click="openEdit(scope.row.id)" v-has-permi="['admin:integralOrder:send']">编辑
            </el-button>
            <el-button icon="el-icon-tickets" size="mini" type="text"
                       @click="openShippingInformation(scope.row.id)"
                       v-has-permi="['admin:integralOrder:shippingInformation']">详情
            </el-button>
            <el-button icon="el-icon-edit" v-if="scope.row.status===4" size="mini" type="text" style="color: #989830;"
                       @click="openRefund(scope.row.orderSn)" v-has-permi="['admin:integralOrder:refund']">退款
            </el-button>
            <el-button icon="el-icon-delete" size="mini" type="text" style="color: red;"
                       @click="handlerDelete(scope.row.id)"
                       v-has-permi="['admin:integralOrder:delete']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :visible.sync="editDialog.show"
        :title="'发货'"
        destroy-on-close
        :close-on-click-modal="false"
        width="1200px"
        top="80px">
      <edit
          v-if="editDialog.show"
          :id="editDialog.id"
          @hideEditDialog="hideEditDialog"
          ref="editForm"
      />
    </el-dialog>
    <el-dialog
        :visible.sync="refundDialog.show"
        :title="'退款'"
        destroy-on-close
        :close-on-click-modal="false"
        width="1200px"
        top="80px">
      <refund
          v-if="refundDialog.show"
          :orderSn="refundDialog.orderSn"
          @hideEditDialog="hideEditDialog"
          ref="editForm"
      />
    </el-dialog>
    <el-dialog
        :visible.sync="shippingInformationDialog.show"
        :title="'订单详情'"
        destroy-on-close
        :close-on-click-modal="false"
        width="1200px"
        top="80px">
      <shippingInformation
          v-if="shippingInformationDialog.show"
          :id="shippingInformationDialog.id"
          @hideEditDialog="hideEditDialog"
          ref="editForm"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit.vue'
import {deleteRow, list, updateStatus, distributionOfPrizes} from "@/api/order";
import {checkPermi} from "@/utils/permission";
import ShippingInformation from "@/views/financial/order/shippingInformation.vue";
import {goodsOrderNews, optionsList} from "@/api/business";
import Refund from "@/views/financial/order/refund.vue";

export default {
  components: {Refund, ShippingInformation, edit},
  data() {
    return {
      listData: {list: [], count: 0},
      listPram: {
        isReceive: '',
        areaId: '',
        page: 1,
        limit: 20,
      },
      editDialog: {
        show: false,
        id: null
      },
      refundDialog: {
        show: false,
        orderSn: null
      },
      shippingInformationDialog: {
        show: false,
        id: null
      },
      tableLoading: false,
      statusList: [{id: 1, data: '待发货'}, {id: 2, data: '待收货'}, {id: 3, data: '已完成 '}, {
        id: 4,
        data: '已申请售后 '
      }, {id: 5, data: '已拒绝 '}, {id: 6, data: '已退款 '}],
      value: [],
      options: [{
        value: '',
        label: '',
        children: [{
          value: '',
          label: '',
          children: [{
            value: '',
            label: ''
          }]
        }]
      }],
     dataStatistics: {}
    }
  },
  mounted() {
    this.getList()
    this.getOptions()
    this.getDataStatistics()
  },
  methods: {
    getDataStatistics(){
      goodsOrderNews().then(res => {
        this.dataStatistics = res.data
      })
    },
    handleChange(value) {
      this.listPram.areaId = value[2]
    },
    getOptions() {
      optionsList().then(res => {
        this.options = res
      })
    },
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        this.listData = res;
      }).finally(() => {
      })
      this.tableLoading = false;
    },
    openEdit(id = null) {
      this.editDialog.id = id;
      this.editDialog.show = true
    },
    openRefund(orderSn = null) {
      this.refundDialog.orderSn = orderSn;
      this.refundDialog.show = true
    },
    openShippingInformation(id = null) {
      this.shippingInformationDialog.id = id;
      this.shippingInformationDialog.show = true
    },
    handlerRelease(id) {
      this.$confirm('是否确认发放').then(() => {
        const loading = this.$loading({text: '发放中', background: 'rgba(255,255,255,.5)'})
        distributionOfPrizes(id, 1).then(() => {
          this.$message.success('发放成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.refundDialog.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    handleStatusChange(shelvesStatus, id) {
      const loading = this.$loading({text: '更新中', background: 'rgba(255,255,255,.5)'})
      updateStatus(id, shelvesStatus).then(() => {
        this.$message.success('更新状态成功')
        this.getList()
      }).finally(() => {
        loading.close()
      })
    },
    resetQuery() {
      this.listPram.status = '';
      this.listPram.areaId = '';
      this.listPram.phone = '';
      this.value = ''
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
